<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">所在科室</label>
                            <div class="layui-input-inline">
                                <input type="text" id="tree" name="deptId" lay-filter="tree" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">  初始化排班 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">移除设置</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">排班</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'treeSelect','table','miniPage','element', 'axios'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var axios = layui.axios;
        var treeSelect= layui.treeSelect;
     //重新加载 table
        window.reload=function(){
            table.reload('currentTableId');
        }

        treeSelect.render({
            // 选择器
            elem: '#tree',
            // 数据
            data: 'http://localhost:9999/dept/queryDeptTree',
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            // 点击回调
            click: function(d){
                console.log(d);
                $("#tree").val(d.current.id)
            },
            // 加载完成后的回调函数
            success: function (d) {
            }
        });

        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:9999/user/queryUserAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                // {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'username', width: 80, title: '用户名'},
                {field: 'sex', width: 80, title: '性别'},
                {field: 'jobTitle', width: 120, title: '职称'},
                {templet:'<div>{{d.dept.name}}</div>',title: '科室', minWidth: 100},
                {field: 'tel', width: 120, title: '电话'},
                {field: 'email', width: 120, title: '邮箱'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var username=data.field.username;
            var deptId=data.field.deptId;
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    username: username,
                    deptId:deptId
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
           if (obj.event === 'add') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(data.length!=1){//如果选中的是多条，暂时不支持 考虑时间问题
                    layer.msg("必须选中一条记录进行初始化");
                }else{
                    //调用获取选中记录的id
                    var ids=getCheackId(data);
                    layer.confirm('确定要初始化吗，如果初始化会覆盖原来的设置信息', function (index) {
                        //像后端调用请求
                        axios.post('work/addInit',{ids:ids}).then(function(response){
                            if(response.data.code==200){
                                layer.msg(response.data.msg);
                                //重新加载表格
                                // window.reload();
                                table.reload('currentTableId');
                            }else{
                                layer.msg(response.data.msg);
                            }
                        }).catch(err=> {
                                console.log(err);
                            }
                        );

                    });
                }
            }else if (obj.event === 'delete') {  // 监听删除操作
               var checkStatus = table.checkStatus('currentTableId')
                   , data = checkStatus.data;
               if (data.length==0) {//如果有选中信息
                   layer.msg("请选择要移除初始化设置的记录信息");
               } else {
                   //调用获取选中记录的id
                   var ids = getCheackId(data);
                   debugger
                   layer.confirm('确定要移除吗？', function (index) {
                       //像后端调用请求
                       axios.post('work/deleteByIds', {ids: ids}).then(function (response) {
                           if (response.data.code == 200) {
                               layer.msg(response.data.msg);
                               //重新加载表格
                               // window.reload();
                               table.reload('currentTableId');
                           } else {
                               layer.msg(response.data.msg);
                           }
                       }).catch(err => {
                               console.log(err);
                           }
                       );

                   });
               }
           }
        });

        /**
         * 获取记录信息的id
         */
        function getCheackId(data){
            var arr=new Array();
            for(var i=0;i<data.length;i++){
                arr.push(data[i].id);
            }
            return arr.join(",");
        }


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });





        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;

            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('page/work/update_init.html?userId=2');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '修改排班',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $("#userId").val(data.id);
                localStorage.removeItem("docId");
                localStorage.setItem("docId",data.id);
                //渲染值信息
                // setFormValues(data);
                document.cookie="name=1243";
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //获取要删除的id 调用相关后端接口即可
                    axios.post('work/deleteByIds', {ids: data.id}).then(function (response) {
                        if (response.data.code == 200) {
                            layer.msg(response.data.msg);
                            //重新加载表格
                            // window.reload();
                            table.reload('currentTableId');
                        } else {
                            layer.msg(response.data.msg);
                        }
                    }).catch(err => {
                            console.log(err);
                        }
                    );


                });
            }
        });

    });
</script>